package com.uzai.console.service.mini.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.mini.MiniOrderStatus;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.BaseDto;
import com.uzai.console.dto.mini.miniorder.MiniOrderIdDto;
import com.uzai.console.dto.mini.miniorder.MiniOrderQuery;
import com.uzai.console.dto.mini.miniorder.MiniOrderSendDto;
import com.uzai.console.entity.MiniOrder;
import com.uzai.console.entity.MiniUser;
import com.uzai.console.entity.UserType;
import com.uzai.console.mapper.MiniOrderMapper;
import com.uzai.console.mapper.MiniUserMapper;
import com.uzai.console.mapper.UserTypeMapper;
import com.uzai.console.service.feign.UzaiCmsFeignService;
import com.uzai.console.service.mini.MiniOrderService;
import com.uzai.console.vo.mini.miniorder.MiniOrderVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class MiniOrderServiceImpl implements MiniOrderService {

    @Autowired
    private MiniOrderMapper miniOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MiniUserMapper miniUserMapper;
    @Autowired
    private UserTypeMapper userTypeMapper;

    /**
     * 分页查询小程序团购商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return MiniOrderVo
     */
    public Page<MiniOrderVo> findMiniOrderByPage(MiniOrderQuery miniOrderQuery){
        //新建返回到页面的LIST对象
        List<MiniOrderVo> miniOrderVoList = new ArrayList<>();
        //返回到页面分页对象
        Page<MiniOrderVo> pageVo = new Page<>();

        //执行分页查询对象
        Page<MiniOrder> page = new Page<>();
        miniOrderQuery.convert(page);
        List<MiniOrder> miniOrderList = miniOrderMapper.findByList(page, miniOrderQuery);

        if(miniOrderList != null && miniOrderList.size() > 0){
            for (MiniOrder miniOrder : miniOrderList){
                MiniOrderVo miniOrderVo = new MiniOrderVo();
                BeanUtils.copyProperties(miniOrder, miniOrderVo);

                if(miniOrder.getStatus() != null){
                    MiniOrderStatus miniOrderStatus = MiniOrderStatus.getByCode(miniOrder.getStatus());
                    if(miniOrderStatus != null){
                        miniOrderVo.setStatusName(miniOrderStatus.getDesc());
                    }
                }

                //会员类型
                if(miniOrder.getUserId() != null){
                    MiniUser miniUser = miniUserMapper.selectById(miniOrder.getUserId(), miniOrder.getMerId());
                    if(miniUser != null){
                        miniOrderVo.setNickName(miniUser.getNickName());
                        miniOrderVo.setUserRemark(miniUser.getRemark());
                        if(miniUser.getUserType() != null){
                            UserType userType = userTypeMapper.selectById(miniUser.getUserType(), miniUser.getMerId());
                            if(userType != null){
                                miniOrderVo.setUserType(miniUser.getUserType());
                                miniOrderVo.setUserTypeName(userType.getName());
                            }
                        }
                    }
                }

                miniOrderVoList.add(miniOrderVo);
            }
        }

        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(miniOrderVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询小程序团购商品详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return MiniOrderVo
     */
    public MiniOrderVo findMiniOrderById(MiniOrderIdDto miniOrderIdDto){

        //返回的对象
        MiniOrderVo miniOrderVo = new MiniOrderVo();

        if(miniOrderIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        MiniOrder miniOrder = miniOrderMapper.selectById(miniOrderIdDto.getId(), miniOrderIdDto.getMerId());
        if(miniOrder == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该订单不存在");
        }

        BeanUtils.copyProperties(miniOrder, miniOrderVo);

        if(miniOrder.getStatus() != null){
            MiniOrderStatus miniOrderStatus = MiniOrderStatus.getByCode(miniOrder.getStatus());
            if(miniOrderStatus != null){
                miniOrderVo.setStatusName(miniOrderStatus.getDesc());
            }
        }

        //会员类型
        if(miniOrder.getUserId() != null){
            MiniUser miniUser = miniUserMapper.selectById(miniOrder.getUserId(), miniOrder.getMerId());
            if(miniUser != null){
                miniOrderVo.setNickName(miniUser.getNickName());
                miniOrderVo.setUserRemark(miniUser.getRemark());
                if(miniUser.getUserType() != null){
                    UserType userType = userTypeMapper.selectById(miniUser.getUserType(), miniUser.getMerId());
                    if(userType != null){
                        miniOrderVo.setUserType(miniUser.getUserType());
                        miniOrderVo.setUserTypeName(userType.getName());
                    }
                }
            }
        }

        return miniOrderVo;
    }

    /**
     * 删除小程序团购商品信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void deleteMiniOrder(MiniOrderIdDto miniOrderIdDto){
        if(miniOrderIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        MiniOrder miniOrder = miniOrderMapper.selectById(miniOrderIdDto.getId(), miniOrderIdDto.getMerId());

        if(miniOrder == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该订单不存在");
        }

        miniOrderMapper.deleteById(miniOrderIdDto.getId(), miniOrderIdDto.getMerId());

    }

    /**
     * 小程序团购订单发货信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void sendMiniOrder(MiniOrderSendDto miniOrderSendDto){
        if(miniOrderSendDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        if(StringUtils.isBlank(miniOrderSendDto.getExpressCompany())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入物流公司");
        }

        if(StringUtils.isBlank(miniOrderSendDto.getExpressNo())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入快递单号");
        }

        MiniOrder miniOrder = miniOrderMapper.selectById(miniOrderSendDto.getId(), miniOrderSendDto.getMerId());

        if(miniOrder == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该订单不存在");
        }

        int now = DateUtil.getNowTime();
        miniOrder.setStatus(MiniOrderStatus.WAIT_RECEIVE.getCode());
        miniOrder.setExpressCompany(miniOrderSendDto.getExpressCompany());
        miniOrder.setExpressNo(miniOrderSendDto.getExpressNo());
        miniOrder.setSenddt(now);
        miniOrder.setUpdateTime(now);
        int j = miniOrderMapper.send(miniOrder);

        if(j != 1){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "网络错误");
        }
    }

    /**
     * 小程序团购订单确认收货
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void confirmReceiveMiniOrder(MiniOrderIdDto miniOrderIdDto){
        if(miniOrderIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        MiniOrder miniOrder = miniOrderMapper.selectById(miniOrderIdDto.getId(), miniOrderIdDto.getMerId());

        if(miniOrder == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该商品不存在");
        }

        //已经收货
        int now = DateUtil.getNowTime();
        miniOrder.setStatus(MiniOrderStatus.COMPLETE.getCode());
        miniOrder.setReceivedt(now);
        miniOrder.setUpdateTime(now);

        miniOrderMapper.confirmReceive(miniOrder);
    }

    /**
     * 小程序团购订单取消
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void cancelMiniOrder(MiniOrderIdDto miniOrderIdDto){
        if(miniOrderIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        MiniOrder miniOrder = miniOrderMapper.selectById(miniOrderIdDto.getId(), miniOrderIdDto.getMerId());

        if(miniOrder == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该商品不存在");
        }

        //已经收货
        int now = DateUtil.getNowTime();
        miniOrder.setStatus(MiniOrderStatus.CANCEL.getCode());
        miniOrder.setUpdateTime(now);

        miniOrderMapper.updateStatus(miniOrder);
    }

}
